//
//  HSOrderDetailViewController.m
//  hsdyOrderSystem
//
//  Created by XunFengKJ on 2017/11/14.
//  Copyright © 2017年 赖国锋. All rights reserved.
//

#import "HSOrderDetailViewController.h"
#import "HSOrderDetailCell.h"
#import <UITableView+FDTemplateLayoutCell.h>
static NSString * const reuseIdentifier = @"orderDetailCell";

@interface HSOrderDetailViewController ()
@property(nonatomic,strong)UIButton *completeOrderBtn;//完成订单
@property(nonatomic,strong)UILabel *countL;//总计
@property(nonatomic,strong)UILabel *orderNumL;//订单号
@property(nonatomic,strong)UILabel *orderTimeL;//订单时间
@property(nonatomic,strong)UIView *footerView;
@property(nonatomic,strong)UILabel *remarkL;
@end

@implementation HSOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"订单详情";
    LGFLog(@"self.model. =  %@",self.model.tableNumber);
}

-(void)initViews{
    
    [self initTableView];
    [self headerRereshing];
}

- (void)initTableView{
    [self initTableViewWithStyle:UITableViewStylePlain];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[NSString stringWithFormat:@"%@",[HSOrderDetailCell class]] bundle:nil ] forCellReuseIdentifier:reuseIdentifier];
    
    self.tableView.backgroundColor = XFUIColorFromRGB(0xf0f2f5);
    [self.view addSubview:self.tableView];
    WEAKSELF
    self.tableView.tableFooterView = self.footerView;
    if (self.model.isHaveReadyUpBtn) {//非已上菜
        [self.view addSubview:self.completeOrderBtn];
        [self.completeOrderBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.equalTo(weakSelf.view);
            make.height.mas_equalTo(44);
        }];
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view).offset(-44);
        }];
    }else{
        [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.right.equalTo(weakSelf.view);
            make.bottom.equalTo(weakSelf.view).offset(0);
        }];
    }
   
    NSInteger shopCount = 0;
    for (int i = 0; i< self.model.foodsList.count;i++) {
        HSOrderDetailModel *detailModel = self.model.foodsList[i];
        shopCount = shopCount + [detailModel.shopCount integerValue];
    }
    self.countL.text =  [NSString stringWithFormat:@"共%zd件商品，总计：%.2f",shopCount,[self roundFloat:[[NSString stringWithFormat:@"%@",self.model.totalPay] floatValue]]];
    self.orderNumL.text = [NSString stringWithFormat:@"订单号:%@",self.model.serial];
//    self.orderTimeL.text =[NSString stringWithFormat:@"下单时间:%@",[NSString timeWithTimeIntervalString:self.model.createTime]];
    if (self.model.isShowComingtime) {
        self.orderTimeL.text =[NSString stringWithFormat:@"到店时间:%@",[NSString timeWithTimeIntervalString:self.model.reservationTime]];
    }else{
        self.orderTimeL.text =[NSString stringWithFormat:@"下单时间:%@",[NSString timeWithTimeIntervalString:self.model.createTime]];
    }
    self.remarkL.text =  [NSString stringWithFormat:@"备注：%@",self.model.extraMessage.length?self.model.extraMessage:@"无"];
}

#pragma mark - click
#pragma mark - 完成订单
-(void)completeOrderBtnClick{
    LGFLog(@"完成订单");
    WEAKSELF
    [UIAlertController alertControllerShowInController:self Title:@"温馨提示" message:@"是否确认完成订单？" checkBlock:^{
        [weakSelf postSureOrderRequest:weakSelf.model.outTradeNo];
    }];
}

#pragma mark - 已上菜
-(void)postSureOrderRequest:(NSString *)outTradeNo{
    [self showLoadingWithClear:YES];
    WEAKSELF
    [XFPostRequest postupdateSureOrderByShopId:[BookShareUserInforDefault readUserUid] outTradeNo:outTradeNo success:^(id responseObject) {
        [weakSelf hideLoading];
        if ([[responseObject safeJsonObjectForKey:@"status"] integerValue] ==200) {
            LGFLog(@"上菜成功");
            lNOTIF_POST(sureOrderSuccessNoti, nil);
            [weakSelf.navigationController popViewControllerAnimated:YES];
        }else{
            [SOAutoHideMessageView showMessage:[responseObject safeJsonObjectForKey:@"msg"] inView:weakSelf.view];
        }
    } failure:^(NSError *error) {
        [weakSelf hideLoading];
        [SOAutoHideMessageView showMessage:LNETWORKERROR inView:weakSelf.view];
    }];
}

#pragma mark - 复制订单
-(void)copyOrderNum{
    [SOAutoHideMessageView showMessage:@"复制订单号成功" inView:self.view];
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    NSString *oderNumStr = [self.orderNumL.text substringFromIndex:4];
    pasteboard.string = oderNumStr;
}

#pragma mark - Data Source
- (void)loadDefaultDataSource{
    [super loadDefaultDataSource];
    self.blankHintImage = [UIImage imageNamed:@""];
    self.blankHintString=@"暂无数据";
}

-(void)loadDataSource{
    [self endPullDownRefreshing];
    //    WEAKSELF
    //    if (self.requestCurrentPage!=1) {
    //        [self.HUD showAnimated:YES];
    //    }
    //    [XFPostRequest postBookMessageWithUid :[BookShareUserInforDefault readUserUid] page:[NSString stringWithFormat:@"%zd",self.requestCurrentPage] success:^(id responseObject) {
    //        LGFLog(@"===%@",responseObject);
    //        [weakSelf hideLoading];
    //        if (responseObject == nil)  return ;
    //        if ([responseObject[@"status"] intValue] == 1003)
    //        {
    //            NSString *pageStr =  [[responseObject[@"msg"] componentsSeparatedByString:@"|"] lastObject];
    //            NSInteger page =  [pageStr integerValue];
    //
    //            [weakSelf addToDataSource:[HSOrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]]];
    //            if (weakSelf.requestCurrentPage == page) {
    //                //                XFMessageModel *model = [XFMessageModel new];
    //
    //                weakSelf.isPageEnd=YES;
    //            }else{
    //                weakSelf.isPageEnd=NO;
    //            }
    //        }
    //        else {
    //            if (![[HSOrderModel mj_objectArrayWithKeyValuesArray:responseObject[@"content"]] isKindOfClass:[NSArray class]]) {
    //                [weakSelf.dataSource removeAllObjects];
    //                [weakSelf reloadDataWithBlank];
    //            }
    //
    //            [weakSelf endPullDownRefreshing];
    //            [weakSelf setIsPageEnd:YES];
    //        }
    //        [weakSelf.tableView reloadData];
    //    } failure:^(NSError *error) {
    //        [weakSelf hideLoading];
    //        [MBProgressHUD showError:LNETWORKERROR];
    //    }];
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    LGFLog(@"self.dataSource.count = %zd",self.model.foodsList.count);
    return self.model.foodsList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HSOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    cell.selectionStyle = 0;
    cell.detailModel = self.model.foodsList[indexPath.row];
    return cell;
}

-(void)configTableView{
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [tableView fd_heightForCellWithIdentifier:reuseIdentifier configuration:^(HSOrderDetailCell *cell) {
             cell.model = self.model;
        }];
}

#pragma maek - lazy
#pragma maek - 完成订单
-(UIView *)completeOrderBtn{
    if (!_completeOrderBtn) {
        _completeOrderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _completeOrderBtn.backgroundColor = LCOLOROFNAV;
        [_completeOrderBtn setTitle:@"完成订单" forState:0];
        _completeOrderBtn.titleLabel.font = FONT(16);
        [_completeOrderBtn addTarget:self touchUpInsideAction:@selector(completeOrderBtnClick)];
    }
    return _completeOrderBtn;
}

#pragma maek - footerView
-(UIView *)footerView{
    if (!_footerView) {
        _footerView = [UIView new];
        _footerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 90+44);
        _footerView.backgroundColor = LCOLOROFBACK;
        
        UIView *footerViewRemark = [UIView new];
        footerViewRemark.backgroundColor = lColorWhite;
        footerViewRemark.frame = CGRectMake(0, 0, SCREEN_WIDTH, 44);
        [_footerView addSubview:footerViewRemark];
        
        UILabel *remarkL = [UILabel new];
        self.remarkL = remarkL;
        remarkL.text = @"备注";
        remarkL.font = FONT(16);
        [footerViewRemark addSubview:remarkL];
        [remarkL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(footerViewRemark);
            make.left.equalTo(footerViewRemark).offset(12);
        }];
        
        
        UIView *footerViewCount = [UIView new];
        footerViewCount.backgroundColor = lColorWhite;
        footerViewCount.frame = CGRectMake(0, 46, SCREEN_WIDTH, 39);
        
        UILabel *countL = [UILabel new];
        self.countL = countL;
//        countL.text = @"共X件商品 合计￥1111元";
        countL.font = FONT(12);
        [footerViewCount addSubview:countL];
        [countL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(footerViewCount);
            make.right.equalTo(footerViewCount).offset(-12);
        }];
        //订单号
        UIView *footerViewOrderInfo = [UIView new];
        footerViewOrderInfo.backgroundColor = lColorWhite;
        footerViewOrderInfo.frame = CGRectMake(0, 46+39, SCREEN_WIDTH, 50);
        UILabel *orderNumL = [UILabel new];
        self.orderNumL = orderNumL;
//        orderNumL.text = @"订单号：1234567890123456789";
        orderNumL.font = FONT(12);
        orderNumL.textColor = XFUIColorFromRGB(0x666666);
        [footerViewOrderInfo addSubview:orderNumL];
        [orderNumL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(footerViewOrderInfo).offset(6);
            make.left.equalTo(footerViewOrderInfo).offset(6);
        }];
        //下单时间
        UILabel *orderTimeL = [UILabel new];
        self.orderTimeL = orderTimeL;
//        orderTimeL.text = @"下单时间：1212-09-09 12：12：12";
        orderTimeL.textColor = XFUIColorFromRGB(0x666666);
        orderTimeL.font = FONT(12);
        [footerViewOrderInfo addSubview:orderTimeL];
        [orderTimeL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(orderNumL.mas_bottom).offset(8);
            make.left.equalTo(footerViewOrderInfo).offset(6);
        }];
        //复制
        UIButton *copyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [footerViewOrderInfo addSubview:copyBtn];
        [copyBtn setTitle:@"复制" forState:0];
        copyBtn.titleLabel.font = FONT(13);
        [copyBtn setTitleColor:XFUIColorFromRGB(0x666666) forState:0];
        copyBtn.layer.borderColor = XFUIColorFromRGB(0x666666).CGColor;
        copyBtn.layer.borderWidth = 1.0f;
        [copyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(footerViewOrderInfo).offset(-5);
            make.height.mas_equalTo(20);
            make.width.mas_equalTo(40);
            make.centerY.equalTo(orderNumL);
        }];
        [copyBtn addTarget:self touchUpInsideAction:@selector(copyOrderNum)];
        [_footerView addSubview:footerViewCount];
        [_footerView addSubview:footerViewOrderInfo];
    }
    return _footerView;
}
//除100区两位小数
-(float)roundFloat:(float)price{
    return  round(price * 100) / 10000;
}
@end
